using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;

namespace ZX.BLL.ClientMobi.Message
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class MsgReplyInfo
    {
        #region 属性
        private String _iD = Guid.NewGuid().ToString();
        /// <summary>
        /// 
        /// </summary>
        public String ID
        {
            get{return _iD;}
            set{_iD = value;}
        }
        
        private String _msgID = Guid.NewGuid().ToString();
        /// <summary>
        /// 
        /// </summary>
        public String MsgID
        {
            get{return _msgID;}
            set{_msgID = value;}
        }
        
        private String _userID = Guid.NewGuid().ToString();
        /// <summary>
        /// 
        /// </summary>
        public String UserID
        {
            get{return _userID;}
            set{_userID = value;}
        }
        
        private string _content = "";
        /// <summary>
        /// 
        /// </summary>
        public string Content
        {
            get{return _content;}
            set{_content = value;}
        }
        
        private DateTime _replyTime = DateTime.Parse("1900-1-1");
        /// <summary>
        /// 
        /// </summary>
        public DateTime ReplyTime
        {
            get{return _replyTime;}
            set{_replyTime = value;}
        }

        public string ReplyTimeToString
        {
            get {
               return _replyTime.ToString("yyyy-MM-dd");
            }
        }
        
        private int _isRead = 0;
        /// <summary>
        /// 
        /// </summary>
        public int IsRead
        {
            get{return _isRead;}
            set{_isRead = value;}
        }

        private int _ChangeType = 0;
        public int ChangeType
        {
            get { return _ChangeType; }
            set { _ChangeType = value; }
        }

        private string _UserName;

        public string UserName
        {
            get { return _UserName; }
            set { _UserName = value; }
        }

        private string _DeptDescription;
        public string DeptDescription
        {
            get { return _DeptDescription; }
            set { _DeptDescription = value; }
        }

        #endregion
        
        #region 构造函数
        public MsgReplyInfo()
        {}
        
        public MsgReplyInfo
        (
            String iD,
            String msgID,
            String userID,
            string content,
            DateTime replyTime,
            int isRead
        )
        {
            _iD = iD;
            _msgID = msgID;
            _userID = userID;
            _content = content;
            _replyTime = replyTime;
            _isRead = isRead;
        }
        #endregion
        
        #region 填充数据实体
        /// <summary>
        /// 得到MsgReplyInfo数据实体
        /// </summary>
        /// <param name="row">row</param>
        /// <returns>MsgReplyInfo数据实体</returns>
        public static MsgReplyInfo PopulateFromDataRow(DataRow row)
        {
            MsgReplyInfo obj = new MsgReplyInfo();
            if (row != null)
            {
                obj.ID = row["ID"] == DBNull.Value ? Guid.NewGuid().ToString() : Convert.ToString(row["ID"]);   
                obj.MsgID = row["MsgID"] == DBNull.Value ? Guid.NewGuid().ToString() : Convert.ToString(row["MsgID"]);   
                obj.UserID = row["UserID"] == DBNull.Value ? Guid.NewGuid().ToString() : Convert.ToString(row["UserID"]);   
                obj.Content = row["Content"] == DBNull.Value ? "" : Convert.ToString(row["Content"]);   
                obj.ReplyTime = row["ReplyTime"] == DBNull.Value ? DateTime.Parse("1900-1-1") : Convert.ToDateTime(row["ReplyTime"]);
                obj.IsRead = row["IsRead"] == DBNull.Value ? 0 : Convert.ToInt32(row["IsRead"]);
                obj.ChangeType = row["ChangeType"] == DBNull.Value ? 0 : Convert.ToInt32(row["ChangeType"]);
                obj.UserName = row["UserName"] == DBNull.Value ? "" : row["UserName"].ToString();
                obj.DeptDescription = row["DeptDescription"] == DBNull.Value ? "" : Convert.ToString(row["DeptDescription"]);
            }
            else
            {
                return null;
            }
            return obj;
        }
        
        /// <summary>
        /// 得到MsgReplyInfo数据实体
        /// </summary>
        /// <param name="reader">reader</param>
        /// <returns>MsgReplyInfo数据实体</returns>
        public static MsgReplyInfo PopulateFromReader(IDataReader reader)
        {
            MsgReplyInfo obj = new MsgReplyInfo();
            
            obj.ID = reader["ID"] == DBNull.Value ? Guid.NewGuid().ToString() : Convert.ToString(reader["ID"]);
            obj.MsgID = reader["MsgID"] == DBNull.Value ? Guid.NewGuid().ToString() : Convert.ToString(reader["MsgID"]);
            obj.UserID = reader["UserID"] == DBNull.Value ? Guid.NewGuid().ToString() : Convert.ToString(reader["UserID"]);
            obj.Content = reader["Content"] == DBNull.Value ? "" : Convert.ToString(reader["Content"]);
            obj.ReplyTime = reader["ReplyTime"] == DBNull.Value ? DateTime.Parse("1900-1-1") : Convert.ToDateTime(reader["ReplyTime"]);
            obj.IsRead = reader["IsRead"] == DBNull.Value ? 0 : Convert.ToInt32(reader["IsRead"]);
            obj.DeptDescription = reader["DeptDescription"] == DBNull.Value ? "" : Convert.ToString(reader["DeptDescription"]);
            return obj;
        }
        
        /// <summary>
        /// 得到MsgReply，DataTable
        /// </summary>
        /// <param name="info">info</param>
        /// <returns>MsgReply，DataTable</returns>
        public static DataTable PopulateFromInfo(MsgReplyInfo info)
        {
            DataTable table = new DataTable("MsgReply");
            DataRow row = null;
            
            if (info != null)
            {
                
                table.Columns.Add("ID", typeof(String));
                table.Columns.Add("MsgID", typeof(String));
                table.Columns.Add("UserID", typeof(String));
                table.Columns.Add("Content", typeof(string));
                table.Columns.Add("ReplyTime", typeof(DateTime));
                table.Columns.Add("IsRead", typeof(int));
            
                row = table.NewRow();
            
                row["ID"] = info.ID;
                row["MsgID"] = info.MsgID;
                row["UserID"] = info.UserID;
                row["Content"] = info.Content;
                row["ReplyTime"] = info.ReplyTime;
                row["IsRead"] = info.IsRead;
                table.Rows.Add(row);
            }

            return table;
        }
        
        /// <summary>
        /// 得到MsgReply，DataTable
        /// </summary>
        /// <param name="infoList">infoList</param>
        /// <returns>MsgReply，DataTable</returns>
        public static DataTable PopulateFromInfoList(List< MsgReplyInfo> infoList)
        {
            DataTable table = new DataTable("MsgReply");
            DataRow row = null;
            
            if (infoList != null)
            {
                
                table.Columns.Add("ID", typeof(String));
                table.Columns.Add("MsgID", typeof(String));
                table.Columns.Add("UserID", typeof(String));
                table.Columns.Add("Content", typeof(string));
                table.Columns.Add("ReplyTime", typeof(DateTime));
                table.Columns.Add("IsRead", typeof(int));
                
                for(int i = 0; i < infoList.Count; i++)
                {
                    row = table.NewRow();
                
                    row["ID"] = infoList[i].ID;
                    row["MsgID"] = infoList[i].MsgID;
                    row["UserID"] = infoList[i].UserID;
                    row["Content"] = infoList[i].Content;
                    row["ReplyTime"] = infoList[i].ReplyTime;
                    row["IsRead"] = infoList[i].IsRead;
                    table.Rows.Add(row);
                }
            }

            return table;
        }
        
        /// <summary>
        /// Copy得到MsgReplyInfo，对象
        /// </summary>
        /// <param name="data">data</param>
        /// <returns>MsgReply，对象</returns>
        public static MsgReplyInfo CopyData<T>(T data) where T : class
        {
            if (data == null) return null;
            
            MsgReplyInfo info = new MsgReplyInfo();
            PropertyInfo[] infoProList = info.GetType().GetProperties();
            PropertyInfo[] dataProList = data.GetType().GetProperties();
            
            foreach (PropertyInfo infoPro in infoProList)
            {
                foreach (PropertyInfo dataPro in dataProList)
                {
                    if (infoPro.Name == dataPro.Name && infoPro.PropertyType.FullName == dataPro.PropertyType.FullName)
                    {
                        infoPro.SetValue(info, dataPro.GetValue(data, null), null);
                    }
                }
            }
            
            return info;
        }
        
        #endregion
    }
}


